{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 数据填充",
   "id": "b7a5beb76156b692"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-10T08:18:05.427661Z",
     "start_time": "2025-09-10T08:18:04.670959Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "from dateutil.relativedelta import relativedelta\n",
    "\n",
    "path = 'D:/2506A/monty03/day13/file/'"
   ],
   "id": "66d5525596b1c22e",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-10T08:26:09.830931Z",
     "start_time": "2025-09-10T08:26:09.807350Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '自动填充.xlsx',\n",
    "                   skiprows=6, # 跳过前6行\n",
    "                   usecols='D:G' ,# 从D列读取到G列\n",
    "                   index_col=None,# 不设置索引列\n",
    "                   dtype={'序号':str,'性别':str,'日期':str} # 设置数据类型\n",
    "                )\n",
    "# print(df)\n",
    "# 演示 at() 方法 获取数据\n",
    "# 获取 谭鑫宇\n",
    "print(df.at[1,'姓名'])\n",
    "# print(df.iloc[1,1])\n",
    "# print(df.loc[1,'姓名'])\n",
    "\n",
    "# 演示 at() 方法修改数据\n",
    "# df.iloc[1,1] = '老谭'\n",
    "# df.loc[1,'姓名'] = '老谭'\n",
    "# df.at[1,'姓名'] = '老谭'\n",
    "# print(df)"
   ],
   "id": "52dedf1f44777cff",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "谭鑫宇\n",
      "    序号   姓名   性别   日期\n",
      "0  NaN  韩耀祖  NaN  NaN\n",
      "1  NaN   老谭  NaN  NaN\n",
      "2  NaN  聂茹凤  NaN  NaN\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-10T08:45:29.804224Z",
     "start_time": "2025-09-10T08:45:29.788894Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 自动填充数据\n",
    "# print(df)\n",
    "from datetime import datetime\n",
    "from dateutil.relativedelta import relativedelta\n",
    "\n",
    "# 使用 dateutil智能的累加月\n",
    "def accumulation_month(start_date,days,month):\n",
    "    # 判断输入的月份是否是字符串，如果是字符串，转换成datetime\n",
    "    if isinstance(start_date,str):\n",
    "        start_date = datetime.strptime(start_date, '%Y-%m-%d')\n",
    "        # return start_date + relativedelta(months=add_month)\n",
    "        return start_date + relativedelta(days=days,months=month)\n",
    "\n",
    "start_date = '2024-12-30'\n",
    "\n",
    "for i in df.index:\n",
    "    df['序号'].at[i] = i + 1\n",
    "    df['性别'].at[i] = '男' if i==0 else '女'\n",
    "    df['日期'].at[i] = accumulation_month(start_date,i,i)\n",
    "\n",
    "print(df)"
   ],
   "id": "e4f0e8b106003f8d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  序号   姓名 性别                   日期\n",
      "0  1  韩耀祖  男  2024-12-30 00:00:00\n",
      "1  2   老谭  女  2025-01-31 00:00:00\n",
      "2  3  聂茹凤  女  2025-03-02 00:00:00\n"
     ]
    }
   ],
   "execution_count": 30
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
